package com.dao;

import com.entity.Td_tblPartyOrgBranch;
import com.entity.Td_tblpartyorgmembers;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

public interface PartyOrgBranchDao extends JpaRepository<Td_tblPartyOrgBranch,Integer> {

    /**
     * 根据党组织id增加一个党员
     * @param branchId 党组织id
     * @return
     */
    @Query(value = "update Td_tblPartyOrgBranch set fd_partycount=fd_partycount+1 where fd_branchId=?",nativeQuery = true)
    public int addMember(int branchId);

    /**
     * 获取所有正式的党组织
     * @return
     */
    @Query(value = "select * from Td_tblPartyOrgBranch WHERE fd_applytype=0 and Fd_applystatus=0",nativeQuery = true)
    public List<Td_tblPartyOrgBranch> getAllBrnch();

    /**
     * 查询所有下级组织成立申请
     * @param parentBranchId 组织id
     * @return
     */
    @Query(value = "select fd_branchid,fd_branchname,fd_applydate from Td_tblPartyOrgBranch where Fd_applystatus=1 and fd_ParentBranchID=? and fd_branchName like ?",nativeQuery = true)
    public List<Object[]> getNewBranchByParent(int parentBranchId,String branchName, Pageable pageable);

    /**
     * 查询所有组织成立申请
     * @return
     */
    @Query(value = "select * from Td_tblPartyOrgBranch where Fd_applystatus=1",nativeQuery = true)
    public List<Td_tblPartyOrgBranch> getNewBranch(Pageable pageable);

    /**
     * 审批通过组织成立申请
     * @param branchId
     * @return
     */
    @Query(value = "update Td_tblPartyOrgBranch set Fd_applystatus=0,fd_applytype=0,fd_approveperson=?,fd_approvedate=getDate() where fd_BranchID=?",nativeQuery = true)
    @Modifying
    public int agreeSetUp(String userName,int branchId);

    /**
     * 查询所有不同意通过的组织
     * @param branchId
     * @return
     */
    @Query(value = "select * from Td_tblPartyOrgBranch where fd_applytype=2 and fd_ParentBranchID=?",nativeQuery = true)
    public List<Td_tblPartyOrgBranch> getOppisitionSetUp(int branchId,Pageable pageable);

    /**
     * 查询本组织和下属组织
     * @param branchId 本组织id
     * @param parentId 父级组织id
     * @param branchName 组织名
     * @param pageable
     * @return 0=组织id   1=组织名   2=成立日期
     */
    @Query(value = "SELECT\n" +
            "	fd_branchid,\n" +
            "	fd_branchname,\n" +
            "	fd_approvedate\n" +
            "FROM\n" +
            "	td_tblpartyorgbranch\n" +
            "WHERE\n" +
            "	Fd_applystatus = 0\n" +
            "AND fd_applytype = 0\n" +
            "AND (\n" +
            "	fd_branchId = ?\n" +
            "	OR fd_parentbranchid = ?\n" +
            ")\n" +
            "AND fd_branchName LIKE ?",nativeQuery = true)
    public List<Object[]> getByParent(int branchId,int parentId,String branchName,Pageable pageable);

    /**
     * 根据组织id查询该组织详情
     * @param branchId 组织id
     * @return
     */
    @Query(value = "select * from Td_tblPartyOrgBranch  where fd_BranchID=?",nativeQuery = true)
    public Td_tblPartyOrgBranch getBybranchId(int branchId);

    /**
     * 查询所有下级组织
     * @param parentId 父级组织id
     * @return 0=组织id   1=组织名   2=成立日期
     */
    @Query(value = "select fd_branchid,fd_branchname,fd_approvedate from Td_tblPartyOrgBranch where fd_ParentBranchID=? and fd_branchName like '%%'",nativeQuery = true)
    public List<Object[]> getByParentId(int parentId,String branchName,Pageable Pageable);
}
